package com.mymvc.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ClassName: MyHandleInterceptor01
 * Package: com.mymvc.interceptor
 * Description:
 *
 * @Author 刘英杰
 * @Create 2025/2/11 21:36
 * @Version 1.0
 */

public class MyHandlerInterceptor02 implements HandlerInterceptor {
    /**
     * 在Handler执行之前执行,例如:可以利用preHandle判断用户是否有权限访问当前页面(权限检测)
     * @param request
     * @param response
     * @param handler
     * @return  如果返回false,那么将不会执行Handler,返回true,才会放行执行Handler
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("....preHandle02....");
        return true;
    }

    /**
     * 在Handler中方法执行return 返回值之前执行,例如:我们可以用来对请求域中的数据进行操作
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println(modelAndView.getModel().get("id"));
        System.out.println("....postHandle02....");
    }

    /**
     * 在Handler方法执行之后再执行 例如:将统一的资源释放,统一的日志记录等
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("....afterCompletion02....");
    }
}
